{% load static %}
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <title>广告管理</title>
    <link rel="stylesheet" href="{% static '/public/css/ui.css' %}">
    <link rel="stylesheet" href="{% static '/public/admin/css/layout.css' %}">
    <script src="{% static '/public/js/jquery.js' %}"></script>
    <script src="{% static '/public/js/ui.js' %}"></script>
</head>

<body>
    <div class="position">当前位置：扩展管理 > <a href="/manager/advert/index">广告管理</a></div>
    <div class="border">
        <!---->
        <a href="/manager/advert/add" class="ui-btn ui-btn-info">添加广告</a>
        <form method="post" class="ui-form">
            <div class="ui-table-wrap">
                <table class="ui-table ui-table-border ui-table-hover ui-table-striped ui-mb ui-mt">
                    <thead class="ui-thead-gray">
                        <tr>
                            <th width="80">排序</th>
                            <th width="80">ID</th>
                            <th>广告名称</th>
                            <th width="80">状态</th>
                            <th width="150">操作</th>
                        </tr>
                    </thead>
                    <tbody>

                        {% if not advert_items %}
                        <tr>
                            <td colspan="5">暂无资料</td>
                        </tr>
                        {% endif %}
                        {% for advert in advert_items %}
                        <tr>
                            <td><input type="hidden" name="mid[]" value="{{advert.id}}"><input type="text"
                                    class="ui-form-ip" name="ordnum[]" id="ordnum_{{advert.id}}"
                                    value="{{advert.ordnum}}" data-rule="required;int;"></td>
                            <td>{{advert.id}}</td>
                            <td class="ui-text-left">{{advert.title}}</td>
                            <td><label class="ui-switch ui-switch-info"><input type="checkbox" {% if advert.islock == 1 %}
                                        checked{% endif %} data-url="/manager/advert/switchs/?id={{advert.id}}"><span
                                        class="ui-switch-checkbox ui-switch-text"></span></label></td>
                            <td><a href="{% url 'manager:advert_edit' advert.id %}"><span class="ui-icon-edit"></span>
                                    编辑</a>{% if advert.akey == '' %}　<a href="javascript:;" class="del"
                                    data-url="/manager/advert/del?id={{advert.id}}"><span class="ui-icon-delete"></span>
                                    删除</a>{% endif %}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% if advert_items %}
            {% csrf_token %}
            <button type="submit" class="ui-btn ui-btn-yellow">保存排序</button>
            {% endif %}
        </form>
        <!---->
    </div>

    <script>
        $(function () {
            const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
            $('.ui-switch input[type=checkbox]').on('click', function () {
                var url = $(this).attr("data-url");
                var result = ($(this).is(':checked')) ? 1 : 0;
                $.ajax(
                    {
                        url: url,
                        type: "post",
                        dataType: 'json',
                        headers: {'X-CSRFToken': csrftoken},
                        mode: 'same-origin',
                        data: "islock=" + result,
                        error: function (e) { alert(e.responseText); },
                        success: function (d) {
                            if (d.state == 'success') {
                                wmcms.success(d.msg);
                            }
                            else {
                                wmcms.error(d.msg);
                            }
                        }
                    });
            });
            $(".ui-form").form(
                {
                    type: 2,
                    result: function (form) {
                        $.ajax(
                            {
                                type: 'post',
                                cache: false,
                                dataType: 'json',
                                url: '{% url "manager:advert_index" %}',
                                data: $(form).serialize(),
                                error: function (e) { alert(e.responseText); },
                                success: function (d) {
                                    if (d.state == 'success') {
                                        wmcms.success(d.msg);
                                        setTimeout(function () { location.href = '{% url "manager:advert_index" %}'; }, 1500);
                                    }
                                    else {
                                        wmcms.error(d.msg);
                                    }
                                }
                            });
                    }
                });
            $(".del").click(function () {
                var url = $(this).attr("data-url");
                $.dialog(
                    {
                        'title': "操作提示",
                        'text': "确定要删除？不可恢复！",
                        'oktheme': 'ui-btn-info',
                        'ok': function (e) {
                            $.ajax(
                                {
                                    url: url,
                                    type: 'post',
                                    dataType: 'json',
                                    data: 'token={$token}',
                                    error: function (e) { alert(e.responseText); },
                                    success: function (d) {
                                        e.close();
                                        if (d.state == 'success') {
                                            wmcms.success(d.msg);
                                            setTimeout(function () { location.href = '{THIS_LOCAL}'; }, 1000);
                                        }
                                        else {
                                            wmcms.error(d.msg);
                                        }
                                    }
                                });
                        }
                    });
            });
        })
    </script>
</body>

</html>